using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Microblaze.Elf
{
  public class ElfSymbolTable : ElfFormatEntity
  {
    private ElfSectionHeader sectionHeader;

    private List<ElfSymbolTableEntry> entries = new List<ElfSymbolTableEntry>();

    public List<ElfSymbolTableEntry> Entries
    {
      get { return entries; }
      set { entries = value; }
    }

    public ElfSymbolTable(ElfSectionHeader section)
      : base(section.ElfFile)
    {
      this.sectionHeader = section;
    }

    public void Read()
    {
      // store old position
      long position = ElfFile.Reader.BaseStream.Position;

      // go to the position of the symbol table entries
      ElfFile.Reader.BaseStream.Seek(sectionHeader.Sh_offset, 0);

      uint entryCount = sectionHeader.Sh_size / sectionHeader.Sh_entsize;

      // read all entries
      for (int i = 0; i < entryCount; i++)
      {
        ElfSymbolTableEntry entry = 
          new ElfSymbolTableEntry(ElfFile);

        entry.Read();

        entries.Add(entry);
      }

      // restore position
      ElfFile.Reader.BaseStream.Seek(position, 0);
    }

    public void DumpToConsole()
    {
      Console.WriteLine("Symbol table (entries: {0})", sectionHeader.Sh_entsize);
      foreach (ElfSymbolTableEntry entry in entries)
        Console.WriteLine("   " + entry.ToString());
    }
  }
}
